package cn.itlaobing.web.servlet;

import cn.itlaobing.exception.MyException;
import cn.itlaobing.model.PanationVir;
import cn.itlaobing.model.VIRDto;
import cn.itlaobing.model.VegetableImportRecordModel;
import cn.itlaobing.model.VegetableTypesModel;
import cn.itlaobing.service.VIRService;
import cn.itlaobing.service.VTypeService;
import cn.itlaobing.util.Blank;
import cn.itlaobing.web.annotation.RequestMapping;
import com.alibaba.fastjson.JSONArray;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static cn.itlaobing.model.PanationVir.FAULT_SIZE;

/**
 * Created by Administrator on 2017-11-10.
 */
@WebServlet(value = "/vir/*")
public class VIRServlet extends BaseServlet {
    private Logger logger=Logger.getLogger(VIRServlet.class);
    private static VIRService virService=new VIRService();
    @RequestMapping("/queryall")
    public void QueryAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String Index=request.getParameter("index");
        if(Blank.isBlank(Index))
        {
            Index="1";
        }
        Integer index=Integer.parseInt(Index);
        String Size=request.getParameter("size");
        if(Blank.isBlank(Size))
        {
            Size=FAULT_SIZE+"";
        }
        Integer size=Integer.parseInt(Size);
        PanationVir panationVir= null;
        try {
            panationVir=virService.QueryAll(index,size);
        } catch (MyException e) {
            e.printStackTrace();
        }
        request.setAttribute("REQUEST_LIST" ,panationVir);
        request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response);

    }
    @RequestMapping("/delete")
    public  void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String ID=request.getParameter("id");
        logger.info(ID);
        int r=0;
        try {
            r=virService.Delete(ID);
        } catch (MyException e) {
            e.printStackTrace();
        }
        PrintWriter out=response.getWriter();
        out.print(r);

    }
    @RequestMapping("/update")
    public void update(HttpServletRequest request,HttpServletResponse response) throws MyException, ServletException, IOException {
        String id=request.getParameter("id");
        String typeid=request.getParameter("typeId");
        String Quantity=request.getParameter("quantity");
        Integer typeId=Integer.parseInt(typeid);
        Float quantity=Float.parseFloat(Quantity);
        VegetableImportRecordModel vegetableImportRecordModel=new VegetableImportRecordModel(id,typeId,quantity);
        int r=0;
        r=virService.update(vegetableImportRecordModel);
        if(r>0)
        {
           request.getRequestDispatcher("/vir/queryall").forward(request,response);
            return ;
        }
        //不成功就还在修改界面，该部分应该在jquery中验证，该步可省略
        request.getRequestDispatcher("/vir/queryall").forward(request,response);


    }
    @RequestMapping("/updateBefore")
    public void updateBefore(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        String id=request.getParameter("id");
        //查询全部的蔬菜种类
       logger.info("我来了");
        VegetableImportRecordModel vegetableImportRecordModel=null;
        VTypeService vTypeService=new VTypeService();
        List<VegetableTypesModel> list=null;
        try {
            list=vTypeService.QueryAll();
            logger.info(list.toString());
            vegetableImportRecordModel=virService.updateBefore(id);
        } catch (MyException e) {
            e.printStackTrace();
        }
        request.setAttribute("VTYPE_SESSION",list);
        request.setAttribute("VMODEL_SESSION",vegetableImportRecordModel);
        request.getRequestDispatcher("/WEB-INF/update.jsp").forward(request,response);

    }
}
